//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.Client
{
    using System.Collections.Generic;
    using System.Windows.Forms;
    using Presenters;
    using ServiceModel.Client;
    using ServiceModel.Client.Storage;
    using ServiceModel.Contract;
    using Views;

    public partial class MainWindow : WindowBase, IMainWindow
    {
        private readonly MainWindowPresenter presenter;

        public MainWindow(IEndpointDescriptionRepository repository)
        {
            this.InitializeComponent();
            this.presenter = new MainWindowPresenter(this, repository, ClientFactory.Create());
        }

        public EndpointDescription SelectedEndpoint
        {
            get
            {
                EndpointDescription item = null;
                this.InvokeAction(() => item = this.endpointListDropDown.SelectedItem as EndpointDescription);
                return item;
            }
        }

        public void SetEndpoints(List<EndpointDescription> items)
        {
            this.endpointListDropDown.SelectedItem = null;
            this.endpointListDropDown.DataSource = items;
        }

        public void DisplayResults(PreflightCheck results)
        {
            this.InvokeAction(() => this.browser.DocumentText = HtmlBuilder.BuildView(results));
        }

        public override void ShowError(string message)
        {
            this.InvokeAction(() => this.browser.DocumentText = HtmlBuilder.BuildErrorView(message));
        }

        private void ClickEditEndpointList(object sender, LinkLabelLinkClickedEventArgs e)
        {
            using (var window = new EditListWindow())
            {
                if (window.ShowDialog(this) == DialogResult.OK)
                {
                    this.presenter.Initialize();
                }
            }
        }

        private void ClickPreflightCheck(object sender, System.EventArgs e)
        {
            this.presenter.StartPreflightCheck();
        }

        private void HandleFormLoad(object sender, System.EventArgs e)
        {
            this.presenter.Initialize();
        }
    }
}
